package Work0604;

import java.io.*;
import java.util.Scanner;

/**
 * 4:获得文件行数的方法 有 bufferedReader.readLine 方法 或者查找byte是否=='\n',比较谁快！
 * 
 * @author Y
 *
 */
public class Compare {

	public static void main(String[] args) throws Exception {
		Scanner in = new Scanner(System.in);
		System.out.println("输入一个文件:");
		String fd = in.next();
		File file = new File(fd);

		System.out.println("开始计时...............");
		long a1 = System.nanoTime();
		BufferedReader b = new BufferedReader(new FileReader(file));
		int q;
		String str;
		for (q = 1; (str = b.readLine()) != null; q++) {
		}
		System.out.println("一共有" + (q - 1) + "行");
		b.close();
		long a2 = System.nanoTime();
		long a = a2 - a1;
		System.out.println("使用bufferedReader.readLine方法的时间为" + a);

		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

		System.out.println("开始计时...............");
		long b1 = System.nanoTime();
		InputStream op = new FileInputStream(file);
		int num = 0;
		byte[] od = new byte[op.available()];
		int oi = op.read(od);
		for (int c = 0; c < oi; c++) {
			if (od[c] == '\n') {
				num++;
			}
		}
		System.out.println("一共有" + (num + 1) + "行");
		op.close();
		long b2 = System.nanoTime();
		long d = b2 - b1;
		System.out.println("使用查找byte是否=='\\n'的时间为" + d);

		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

		System.out.println(a > d ? "第二种方法快!" : "第一种方法快!");
	}

}
